package com.ruicar.afs.cloud.manage.workflow.processor;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.modules.contract.enums.BusinessUrlEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.CaseStatusEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.ContractBusinessEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.ProcessStatusEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.common.entity.ManageApplyRecord;
import com.ruicar.afs.cloud.manage.common.entity.ManageApprovalRecord;
import com.ruicar.afs.cloud.manage.common.entity.ManageUpcomeTasksRecord;
import com.ruicar.afs.cloud.manage.common.service.ManageApplyRecordService;
import com.ruicar.afs.cloud.manage.common.service.ManageApprovalRecordService;
import com.ruicar.afs.cloud.manage.common.service.ManageUpcomeTasksRecordService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 保存审批相关数据
 */
@AllArgsConstructor
@Service
public class ContractWorkFlowSaveApproveInfoProcessor {

    private final ManageApplyRecordService manageApplyRecordService;
    private final ManageUpcomeTasksRecordService manageUpcomeTasksRecordService;
    private final ManageApprovalRecordService manageApprovalRecordService;

    /**
     * 保存审批相关数据
     *
     * @param obj              业务数据对象
     * @param contractBusiness 业务类型
     * @param caseStatus       案件状态
     */
    public void saveApproveInfo(Object obj, ContractBusinessEnum contractBusiness, CaseStatusEnum caseStatus, BusinessUrlEnum url) {
        //保存合同信息变更申请记录总表数据
        ManageApplyRecord manageApplyRecord = new ManageApplyRecord();
        BeanUtils.copyProperties(obj, manageApplyRecord);
        manageApplyRecord.setCreateBy(null);
        ManageApplyRecord applyRecord = manageApplyRecordService.getOne(Wrappers.<ManageApplyRecord>lambdaQuery()
                .eq(ManageApplyRecord::getCaseNo, manageApplyRecord.getCaseNo()));
        if (EmptyUtils.isEmpty(applyRecord)) {
            manageApplyRecord.setId(null);
            manageApplyRecord.setContractBusiness(contractBusiness);
            manageApplyRecord.setApplyTime(DateUtil.date());
            manageApplyRecord.setCaseStatus(caseStatus);
            manageApplyRecordService.save(manageApplyRecord);
        } else {
            BeanUtils.copyProperties(obj, applyRecord, "businessSource", "applyTime", "id");
            applyRecord.setCaseStatus(caseStatus);
            manageApplyRecordService.updateById(applyRecord);
        }

        //保存待办任务信息数据
        ManageUpcomeTasksRecord manageUpcomeTasksRecord = new ManageUpcomeTasksRecord();
        BeanUtils.copyProperties(obj, manageUpcomeTasksRecord);
        String id = String.valueOf(manageUpcomeTasksRecord.getId());
        manageUpcomeTasksRecord.setBizDataId(id);
        ManageUpcomeTasksRecord upcomeTasksRecord = manageUpcomeTasksRecordService.getOne(Wrappers.<ManageUpcomeTasksRecord>lambdaQuery()
                .eq(ManageUpcomeTasksRecord::getCaseNo, manageUpcomeTasksRecord.getCaseNo())
                .eq(ManageUpcomeTasksRecord::getBizDataId, manageUpcomeTasksRecord.getBizDataId()));
        if (EmptyUtils.isEmpty(upcomeTasksRecord)) {
            manageUpcomeTasksRecord.setId(null);
            manageUpcomeTasksRecord.setContractBusiness(contractBusiness);
            manageUpcomeTasksRecord.setBusinessUrl(AfsEnumUtil.key(url));
            manageUpcomeTasksRecordService.save(manageUpcomeTasksRecord);
        } else {
            upcomeTasksRecord.setProcessStatus(manageUpcomeTasksRecord.getProcessStatus());
            manageUpcomeTasksRecordService.updateById(upcomeTasksRecord);
        }
    }

    /**
     * 首节点，保存审批记录共用公用方法
     *
     * @param obj
     * @param businessEnum
     */
    public void saveApproveRecord(Object obj, ContractBusinessEnum businessEnum) {
        ManageApprovalRecord record = new ManageApprovalRecord();
        BeanUtils.copyProperties(obj, record);
        String id = String.valueOf(record.getId());
        record.setId(null);
        record.setBizDataId(id);
        record.setApprovalOpinion("提交");
        record.setReasonType("");
        record.setContractBusiness(businessEnum);
        record.setProcessNode(AfsEnumUtil.key(ProcessStatusEnum.start));
        record.setNodeName(AfsEnumUtil.desc(ProcessStatusEnum.start));
        record.setProcessPerson(SecurityUtils.getUser().getUsername());
        manageApprovalRecordService.save(record);
    }


}
